package com.usf.jprovider.schema.provider;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.builder.SQLBuilder;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.schema.AbstractObject;
import com.usf.jprovider.schema.provider.adapter.AbstractProviderAdapter;
import com.usf.jprovider.schema.provider.dao.AbstractDAOSource;
import com.usf.jprovider.schema.provider.file.AbstractFileSource;
import com.usf.jprovider.schema.provider.list.ProviderList;
import com.usf.jprovider.schema.provider.pointer.AbstractPointer;
import com.usf.jprovider.schema.provider.random.AbstractRandomProvider;
import com.usf.jprovider.schema.type.AbstractDBType;
import com.usf.jprovider.schema.type.DBString;

@XmlSeeAlso({
	EmptyProvider.class,
	AbstractRandomProvider.class,
	AbstractDAOSource.class,
	AbstractFileSource.class,
	AbstractPointer.class,
	AbstractProviderAdapter.class,
	ProviderList.class
})
public abstract class AbstractProvider extends AbstractObject {
	
	@XmlElement protected AbstractDBType type;
	
	protected AbstractProvider() {
		super();
		type = new DBString();
	}
	public AbstractDBType getType() {
		return type;
	}
	
	public boolean check(Environment env, JObjectNode node) throws Exception{
		return checkValue(env, node, node.getValue());
	}	
	public void persist(Environment env, JObjectNode node, SQLBuilder builder) throws Exception {
		if(isDependent(node)) 
			node.changeValue(create(env, node));
		String value = type.formatSQL(env, node.getValue().getFormatedValue());
		builder.add(node.getName(), value);
	}

	public abstract JType<?> create(Environment env, JObjectNode node) throws Exception;
	public abstract boolean checkValue(Environment env, JObjectNode node, JType<?> value) throws Exception;
	public abstract boolean isDependent(JObjectNode node) throws Exception;
}
